<!DOCTYPE html>
<html>
<head th:include="cultivateWeb/header :: culHeaderCss">
</head>
<link rel="stylesheet" href="/css/cultivateWeb/login.css">
<link href="/lib/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<body>
<!-- 顶部导航 -->
<header th:include="cultivateWeb/header :: header"></header>
<style>
    button[disabled], html input[disabled]{
        cursor: not-allowed;background: #666;
    }
</style>
<div class="logPage">
    <div class="layui-container">
    <section class="sec_1">
        <div class="div_1">
            <div class="div_2">
                <span style="width:365px;font-size: 24px;font-weight:bold;text-align: center;margin: 0 auto;">忘记密码</span>
            </div>
            <div style="display: inline" id="log">
                <form id="signupForm">
                    <input type="hidden" name="wxcode" th:value="${wxcode}"/>
                    <div class="div_3">
                        <span class="sp_2">手&nbsp;&nbsp;机&nbsp;&nbsp;号：<input id="mobile" name="mobile" type="text" class="in_1" /></span>
                    </div>
                    <div class="div_3">
                        <span class="sp_2">验&nbsp;&nbsp;证&nbsp;&nbsp;码：<input name="code" id="code" type="text" class="in_2" /><input type="button" onclick="send(this)" class="bt_1" value="获取验证码" />
                            </span>
                    </div>
<!--                    <div class="div_3">-->
<!--                        <span class="sp_2">新&nbsp;&nbsp;密&nbsp;&nbsp;码：<input name="newPassWord" id="newPassWord" type="password" class="in_1" /></span>-->
<!--                    </div>-->
<!--                    <div class="div_3">-->
<!--                        <span class="sp_2">确认密码：<input name="password" id="password" type="password" class="in_1" /></span>-->
<!--                    </div>-->
                    <br/>
                    <div class="div_3">
                        <span class="sp_2"><button type="submit" class="bt_2">提交</button></span>
                    </div>
                </form>
            </div>
        </div>
    </section>
    </div>
</div>
<div class="footer" th:include="cultivateWeb/header :: footer"></div>
</body>
<script src="/js/plugins/layer/layer.min.js" th:src="@{/js/plugins/layer/layer.min.js}"></script>
<script src="/js/plugins/validate/jquery.validate.min.js"></script>
<script src="/js/plugins/validate/jquery.validate.extend.js"></script>

<script type="text/javascript">

    $(function () {
        validateRule();
    });
    $.validator.setDefaults({
        submitHandler: function () {
            login();
        }
    });

    var countdown = 60;
    function settime(val) {
        if (countdown == 0) {
            val.removeAttribute("disabled");
            val.value = "获取验证码";
            countdown = 60;
        } else {
            val.setAttribute("disabled", true);
            val.value = "重新发送(" + countdown + ")";
            countdown--;
            var myVar = setTimeout(function () {
                settime(val)
            }, 1000)
        }
    }

    function checkMobile(sMobile) {
        if (!(/^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(sMobile))) {
            layer.msg('手机号码填写错误，请填写正确的手机格式', {
                icon: 2,
                time: 2000
            });
            document.mobileform.mobile.focus();
            return false;
        }
        return true;
    }

    function send(val) {
        var name = $("#mobile").val();
        if (name.length != 0) {
            $.ajax({
                url: '/certificate/sendRegCodeWx',
                type: 'POST',
                data: {mobile: name, wxId: 103},
                success: function (data) {
                    checkMobile(name);
                    if (data == 1) {
                        val.removeAttribute("disabled");
                        val.value = "获取验证码";
                        countdown = 60;
                        layer.msg("此手机号码已注册", {icon: 0, time: 1000})
                    } else {
                        layer.msg("发送成功");
                        val.setAttribute("disabled", true);
                        val.value = "重新发送(" + countdown + ")";
                        countdown--;
                        setTimeout(function () {
                            settime(val)
                        }, 1000)
                    }
                }
            })
        } else {
            layer.msg('请输入手机号！');
        }
    }


    // function login() {
    //     var password = $("#password").val();
    //     var newPassword = $("#newPassWord").val();
    //     if (password != newPassword){
    //         layer.msg("两次密码输入不一致，请修改后提交！")
    //     }else {
    //         $.ajax({
    //             url: "/jzApi/utils/forgetPassWord",
    //             data:$("#signupForm").serialize(),
    //             success: function (r) {
    //                 if (r.code == 0) {
    //                     layer.msg("修改成功");
    //                         // 登录态失效, 重新登录
    //                     setTimeout(function () {
    //                        location.href = "/cultivate/cultivateLogins?name=0"
    //                     }, 1000)
    //                 } else {
    //                     layer.msg(r.msg);
    //                 }
    //             }
    //         })
    //     }
    // }

    function login() {

        $.ajax({
            type: "POST",
            url: '/jzApi/wximgCode/userAddWxCodeopenId',
            data: $('#signupForm').serialize(),
            success: function (r) {
                console.log("r",r);
                if (r.code == 50010){
                    layer.msg("验证码过期");
                }else if (r.code == 50011){
                    layer.msg("验证码错误");
                }else if(r.code == 50013){
                    layer.msg("手机号未绑定账户");
                }else if(r.code == 50013){
                    layer.msg("用户名已存在");
                } else {
                    window.localStorage.setItem("userName", r.data.userName);
                    window.localStorage.setItem("token", r.data.token);
                    window.localStorage.setItem("unreadNum0", r.data.unreadNum0);
                    window.localStorage.setItem("unreadNum1", r.data.unreadNum1);


                    location.href = "/cultivate/homepage";
                }

            },
        });
    }
    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i>";
        $("#signupForm").validate({
            rules: {
                mobile: {
                    required: true,
                    isPhone:true
                },
                code: {
                    required: true
                }
                // ,
                // newPassWord: {
                //     required: true,
                //     checkPassword1:true,
                //     minlength:8,
                //     maxlength:15
                // },
                // password: {
                //     required: true
                // }
            },
            messages: {
                mobile: {
                    required: icon + "请输入手机号"
                },
                code: {
                    required: icon + "请输入验证码"
                 }
                //,
                // newPassWord: {
                //     required: icon + "请输入新密码",
                //     minlength:'密码最少8位',
                //     maxlength:'密码最长15位'
                // },
                // password: {
                //     required: icon + "请确认新密码"
                // }
            }
        })
    }

</script>
</html>